{extend name='common/common' /}

{block name="style"}
    <link href="__CSS__/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="/static/common/croppic/css/croppic.css" rel="stylesheet">
{/block}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-8">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>编辑信息</h5>
                </div>
                <div class="ibox-content">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">修改头像：</label>
                        <div class="input-group col-sm-4" style="overflow:hidden">
                            <div id="croppic" style="width:200px;height:200px;position:relative;border:1px solid "></div>
                        </div>
                    </div>

					<div class="form-group" style="margin-bottom: 100px;">
						<div class="col-sm-4 col-sm-offset-6">
							<button class="btn btn-primary" type="button" id="upheade">上传</button>
							<button class="btn btn-danger" type="button" id="delheade">删除</button>
						</div>
					</div>

                    <!-- <form class="form-horizontal m-t" id="commentForm" method="post" action="{:url('profile/headEdit')}">
                        <input type="hidden" name="head" id="myOutputId">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">真实姓名：</label>
                            <div class="input-group col-sm-4">
                                <input id="real_name" type="text" class="form-control" name="real_name" value="{$user_data.real_name}" placeholder="请输入真实姓名" required="" aria-required="true" value="">

                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-6">
                                <button class="btn btn-primary" type="submit">提交</button>
                            </div>
                        </div>
                    </form> -->
                </div>
            </div>

        </div>
    </div>
</div>
{/block}


{block name="script"}
<script src="__JS__/plugins/validate/jquery.validate.min.js"></script>
<script src="__JS__/plugins/validate/messages_zh.min.js"></script>
<script src="__JS__/plugins/iCheck/icheck.min.js"></script>
<script src="/static/common/croppic/js/croppic.min.js"></script>
<script type="text/javascript">

    var index = '';
    function showStart(){
        index = layer.load(0, {shade: false});
        return true;
    }

    function showSuccess(res){

        layer.ready(function(){
            layer.close(index);
            if(1 == res.code){
                layer.alert(res.msg, {title: '友情提示', icon: 1, closeBtn: 0}, function(){
                    window.location.href = res.data;
                });
            }else if(111 == res.code){
                window.location.reload();
            }else{
                layer.msg(res.msg, {anim: 6});
            }
        });
    }

    //头像上传插件
    function croppicUp() {
		var ImgPath = null;
        var cropperOptions = {
            loadPicture:'{$user_data.head}',
			uploadUrl:'{:url('admin/profile/uploadHeade')}',
            cropUrl:'{:url('admin/profile/cropHeade')}',
			customUploadButtonId:'upheade',
            outputUrlId:'myOutputId',
			// onBeforeImgUpload:function(){},
		    onAfterImgUpload:function(){
				ImgPath = $("#croppic img").attr('src');
			},
		    // onImgDrag:function(){ console.log('onImgDrag') },
		    // onImgZoom:function(){ console.log('onImgZoom') },
		    // onBeforeImgCrop:function(){ console.log('onBeforeImgCrop') },
		    onAfterImgCrop:function(){
				$('#commentForm').submit();
			},
		    onReset:function(){
				var delPath = $("#croppic img").attr('src');
				if(!delPath){
					delPath = ImgPath;
				}
				$.post("{:url('admin/profile/delheade')}",{delPath:delPath},function(res){
					layer.msg(res.msg);
					if(res.code == 1){
						$('#commentForm').submit();
					}
				});
			},
		    onError:function(errormsg){ console.log('onError:'+errormsg) }
		}
		//绑定div
		var cropperHeader = new Croppic('croppic', cropperOptions);
    }

    $(document).ready(function(){
        croppicUp();

        $(".i-checks").iCheck({checkboxClass:"icheckbox_square-green",radioClass:"iradio_square-green",});

        // 编辑管理员
        var options = {
            beforeSubmit:showStart,
            success:showSuccess
        };

        $('#commentForm').submit(function(){
            $(this).ajaxSubmit(options);
            return false;
        });
    });

    // 表单验证
    $.validator.setDefaults({
        highlight: function(e) {
            $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
        },
        success: function(e) {
            e.closest(".form-group").removeClass("has-error").addClass("has-success")
        },
        errorElement: "span",
        errorPlacement: function(e, r) {
            e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
        },
        errorClass: "help-block m-b-none",
        validClass: "help-block m-b-none"
    });

	$("#delheade").on("click",function(){
		var delPath = $("#croppic img").attr('src');
		$.post("{:url('admin/profile/delheade')}",{delPath:delPath},function(res){
			layer.msg(res.msg);
			if(res.code == 1){
				$('#commentForm').submit();
			}
		});
	})

</script>
{/block}
